package movee.spring.app.template.api.v1;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import movee.spring.app.template.domain.api.CountedNormalResponse;
import movee.spring.app.template.domain.api.NormalResponse;
import movee.spring.app.template.domain.dao.JoinedOrderDao;
import movee.spring.app.template.domain.dao.OrderDao;
import movee.spring.app.template.domain.query.JoinedOrderQuery;
import movee.spring.app.template.domain.query.OrderSearchQuery;
import movee.spring.app.template.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * order管理操作单restful controller层
 *
 * @author movee
 */
@RestController
@RequestMapping(value = "/v1/order", produces = "application/json")
@Slf4j
@Api(tags = "order基本操作")
public class ApiOrderControllerV1 {

    private final OrderService opService;

    @Autowired
    public ApiOrderControllerV1(OrderService opService) {
        this.opService = opService;
    }

    /**
     * 根据指定条件查询表项
     * @param s 查询条件
     * @return 符合条件的表项
     */
    @ApiOperation(value = "查询表项列表", produces = "application/json")
    @PostMapping(value = "/list")
    public CountedNormalResponse<List<OrderDao>> search(@ApiParam(value = "查询条件")
                                                      @RequestBody OrderSearchQuery s) {

        return new CountedNormalResponse<>(opService.search(s));
    }


    /**
     * 根据order id查询order的详细信息
     * @param id order id
     * @return 详细信息
     */
    @ApiOperation("查询详情")
    @GetMapping(value = "/detail")
    public NormalResponse<JoinedOrderDao> getDetailById(@RequestParam Long id) {

        return new NormalResponse<>(opService.getOrderDetailById(id));
    }

    /**
     * 创建或更新order详细信息
     * @param query 创建或更新order的详细信息
     * @return 执行成功或失败信息
     */
    @ApiOperation("创建或更新order详情")
    @PostMapping(value = "/detail")
    public NormalResponse<Long> updateDetail(@RequestBody @Valid JoinedOrderQuery query) {
        return new NormalResponse<>(opService.updateOrderDetail(query));
    }

}
